package com.tzy.shoppingcart.service;

import com.tzy.common.constant.WebResult;
import com.tzy.common.entity.Orders;
import com.tzy.common.entity.ShoppingCart;
import com.tzy.shoppingcart.mapper.ShoppingCartMapper;
import com.tzy.shoppingcart.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author 刘舒
 * @since 2022-03-07
 */
@Service
public class ShoppingCartService extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Override
    public List<ShoppingCart> getList(int userId) {
        return shoppingCartMapper.findList(userId);
    }

    public List<ShoppingCart>findByShop(int userId,int shopId){
        return shoppingCartMapper.findByShop(userId,shopId);
    }

    public ShoppingCart getCart(int cartId){
        return shoppingCartMapper.findCart(cartId);
    }


    /**
     * 修改订单
     * @param shoppingCart
     * @return
     */
    public int update(ShoppingCart shoppingCart){
        return shoppingCartMapper.updateById(shoppingCart);
    }

    /**
     * 添加订单
     * @param shoppingCart
     * @return
     */
    public int insert(ShoppingCart shoppingCart){
        return shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 根据id删除用户订单
     *
     * @param id
     * @return
     */
    public int removeById(int id) {
        return shoppingCartMapper.deleteById(id);
    }

    public int deleteShoppingCartByIds(String[] ids){
        return shoppingCartMapper.deleteShoppingCartByIds(ids);

    }

    /**
     * 修改购物车中商品的数量
     *
     * @param id          购物车数据id
     * @param orderNumber          新的数量
     */
    public int updateNumByCid(Integer id, Integer orderNumber) {
        return shoppingCartMapper.updateNumByCid(id, orderNumber);
    }
    /**
     * 查询某用户在购物车添加的某商品的详情
     *
     * @param userId 用户的id
     * @param dishesId 商品的id
     * @return 匹配的购物车详情，如果该用户没有将该商品添加到购物车，则返回null
     */
    public ShoppingCart findByUidAndPid(Integer userId, Integer dishesId) {
        return shoppingCartMapper.findByUidAndPid(userId, dishesId);
    }
    public int findTotal(int userId){
        return shoppingCartMapper.findTotal(userId);
    }

}
